package org.zfes.snowier.cloud.zbss.widget.act.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.widget.act.service.IActSubjectService;
import org.zfes.snowier.cloud.zbss.widget.act.BizUtil;
import org.zfes.snowier.cloud.zbss.widget.act.dao.ActSubjectMapper;
import org.zfes.snowier.cloud.zbss.widget.act.dto.ActSubjectCreateDto;
import org.zfes.snowier.cloud.zbss.widget.act.model.ActSubject;
import java.util.stream.Stream;
@Service
public class ActSubjectServiceImpl implements IActSubjectService{
 	@Autowired
	private ActSubjectMapper actSubjectMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public  String createActSubject(ActSubjectCreateDto actSubjectDto,Long userId) {
		
		ActSubject actSubject=new ActSubject()
				.setCreateTime(ZDateUtil.newDate())
				.setUpdateTime(ZDateUtil.newDate())
				.setStatus(Byte.valueOf("0"))//未开始
				.setPinNum(generatePin());
		ZBeanUtil.copy(actSubjectDto, actSubject, true);
		
		ZBeanUtil.validateBean(actSubject);
		
		actSubjectMapper.insertSelective(actSubject);
		
		 return actSubject.getPinNum();
	 }
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public  String startActSubject(Long id) {
		
		ActSubject actSubject=loadActSubjectById(id).orElseThrow(()->new ServiceLogicalException("未查询到数据\""));
		
		ZAssert.equals(Byte.valueOf("0"), actSubject.getStatus(), "活动已经结束或者进行中");
		
		setActSubjectStatus( id, Byte.valueOf("1"));
	
		return actSubject.getPinNum();
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public  void endActSubject(Long id) {
		
		ActSubject actSubject=loadActSubjectById(id).orElseThrow(()->new ServiceLogicalException("未查询到数据\""));

		ZAssert.equals(Byte.valueOf("0"), actSubject.getStatus(), "活动已经结束或者进行中");
		
		setActSubjectStatus( id, Byte.valueOf("2"));
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateActSubject(Long subjectId,ActSubjectCreateDto actSubjectDto) {
		
		ActSubject actSubject=loadActSubjectById(subjectId).orElseThrow(()->new ServiceLogicalException("未查询到数据\""));
		
		ZAssert.equals(Byte.valueOf("0"), actSubject.getStatus(), "当前课堂不是【未开始】状态，不允许修改");
		
		ZBeanUtil.copy(actSubjectDto, actSubject, true);
		
		ZBeanUtil.setUpdatedInfo(actSubject);
		
		ZBeanUtil.validateBean(actSubject);
		actSubjectMapper.updateByPrimaryKeySelective(actSubject);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActSubject> loadActSubjectById(Long id) {
		if(id!=null){
			return Optional.ofNullable(actSubjectMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	
	//#0未进行， 1进行中，2已结束 -1删除#
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setActSubjectStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1,2}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		actSubjectMapper.updateStatus( id,  status) ;
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadActSubjectDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		
		pm.getStr("name").ifPresent(val->pm.updateParam("name", "%"+val+"%"));
		pm.getStr("remark").ifPresent(val->pm.updateParam("remark", "%"+val+"%"));
		
		DataSet ds=DataSet.newDS2(actSubjectMapper.selectListCount(pm), actSubjectMapper.selectMapListPage(pm));
		return ds;
	}
	

	
	private String generatePin() {
		List<String> pinNumsRondom=BizUtil.randomChars(20, 6);
		List<String> pinNums=actSubjectMapper.selectPinNums(BizUtil.randomChars(20, 6),Byte.valueOf("1"));
		pinNumsRondom.removeAll(pinNums);
		if(!pinNumsRondom.isEmpty()) {
			return pinNumsRondom.get(0);
		}else {
			return generatePin();
		}
		
	}

//  //#0未进行， 1进行中，2已结束 -1删除#
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<ActSubject> loadActSubjectByPinNum(String pinNum) {
		if(ZStrUtil.hasNoText(pinNum)) {
			return Optional.empty();
		}
		return Optional.ofNullable(actSubjectMapper.selectByPinNum(pinNum,Byte.valueOf("1")));
	}
	
	
}